<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title>播放m3u8格式</title>
    <link href="https://unpkg.com/video.js/dist/video-js.css" rel="stylesheet">
    <script src="https://unpkg.com/video.js/dist/video.js"></script>
    <script src="https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js"></script>
    <!-- videojs-contrib-hls 用于在电脑端播放 如果只需移动端播放可以不引入 -->
</head>

<body>
    <style>
        .video-js .vjs-tech {
            position: relative !important;
        }
    </style>
    <div>
        <video id="myVideo" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto"
            data-setup='{}' style='width: 100%;height: auto'>
            <source id="source" src="http://127.0.0.1/hls/1.m3u8" type="application/x-mpegURL">
            </source>
        </video>
    </div>
</body>

<script>
    var myVideo = videojs('myVideo', {
        bigPlayButton: true,
        textTrackDisplay: false,
        posterImage: false,
        errorDisplay: false,
    })
    myVideo.play()
    var changeVideo = function (vdoSrc) {
        if (/\.m3u8$/.test(vdoSrc)) {
            myVideo.src({
                src: vdoSrc,
                type: 'application/x-mpegURL'
            })
        } else {
            myVideo.src(vdoSrc)
        }
        myVideo.load();
        myVideo.play();
    }
</script>